<?php

namespace App\Http\Controllers\home;

use App\Models\Brand;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Cart;
use App\Models\Type;
use App\Models\Image;

class IndexController extends Controller
{
    public function index(Request $request)
    {
        if(session()->has('openid')){
           // dd(session('openid'));
        }else{
            $code = $request->input('code', '');
            //dd($code);
            if($code != '') {
                //获取code码，以获取openid
                $openid = WXTool::getOpenid($code);
                //查询数据库中是否有该微信用户
                $res=User::where('openid',$openid)->first();
                if(!$res){

                }
                // 将openid保存到session
                $request->session()->put('openid', $openid);
            }

        }

        //遍历分类
        $type=new Type();
        $list=$type->getone();

        $img=new Image();
        $bannerlist=$img->where('type',2)->get();
        $rexiao=$img->where('type',3)->get();

        //获取品牌
        $brand=Brand::where('hod','1')->paginate(4);
        return view('home/index',['type'=>$list,'brand'=>$brand,'banner'=>$bannerlist,'rexiao'=>$rexiao]);
    }

    public function liaochenggou()
    {
        //检测是否有登录
        if(session()->has('homeuser')) {
            $cart = new Cart();
            $bo= $cart->donum();
            if($bo){
                $num = $bo;
            }else{
                $num = 0;
            }

        }else{
            $num = 0;
        }
        $img=new Image();
        $rexiao=$img->where('type',3)->get();
        return view('home/liaochenggou',['num'=>$num,'list'=>$rexiao]);
//        return '111';
    }


}
